<template>
  <el-row type="flex" justify="space-btween" align="middle" style="height: 40px; width:100%">
    <el-col>
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>负责人</el-col>
        <el-col>
          <el-dropdown @command="operateDepts">
            <span>操作
              <i class="el-icon-arrow-down" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除子部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments.js'
export default {
  props: {
    treeNode: {
      required: true,
      type: Object
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 点击 删除 新增 编辑 按钮的时候触发事件
    async operateDepts(type) {
      if (type === 'add') {
        // 新增逻辑
        // 通知父组件新增部门
        this.$emit('addDepts', this.treeNode)
      } else if (type === 'edit') {
        // 编辑逻辑
        // 通知父组件编辑部门
        this.$emit('editDepts', this.treeNode)
      } else {
        // 删除逻辑
        try {
          await this.$confirm('是否确定删除改部门?', '提示', { // 弹框提示用户删除操作,利用await等待用户点击确认之后调用接口处理删除
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          await delDepartments(this.treeNode.id)// 调用删除逻辑
          this.$message.success('删除成功')// 提示删除成功
          this.$emit('delDepts')// 通知父组件重新获取后端数据,渲染数据
        } catch (error) {
          console.log(error)
        }
      }
    }
  }

}
</script>

<style>

</style>
